<?php

namespace app\api\controller;

use app\api\model\Circles;
use app\api\model\DesignCategory;
use app\api\model\Designs;
use app\api\model\DesignServices;
use app\api\model\Orders;
use app\api\model\UserGrade;
use app\api\model\Users;
use app\api\model\UserVip;
use app\api\validate\CreateDesignValidate;
use app\common\controller\Api;
use think\Controller;
use think\Request;
use function fast\e;

/**
 * 设计师
 */
class Design extends Api
{
    protected $noNeedLogin = ['index', 'selectSubscribe'];
    protected $noNeedRight = ['*'];

    /**
     * 分类
     */
    public function category()
    {
        $categories = DesignCategory::getCategories();
        $categories ? $this->success('请求成功', compact('categories')) : $this->error('没有数据', [], 2);
    }

    /**
     * 首页造型师
     */
    public function index()
    {
        $category_id = $this->request->get('category_id');
        $designs = Designs::getDesigns($category_id);
        if (!$designs) {
            $this->error('没有数据', [], 2);
        }
        foreach ($designs as $val) {
            //最低价
            $min_amount = DesignServices::getDesignMin($val->id);
            //会员价
            $discount = Users::getVipDiscount($this->auth->getUserId());
            $vip_amount = $min_amount * $discount;
            $val->min_amount = numberFormat($min_amount);
            $val->vip_amount = numberFormat($vip_amount);
        }
        $this->success('请求成功', ['designs' => $designs]);
    }

    /**
     * 预约选择页面
     */
    public function selectSubscribe($design_id)
    {
        $design = Designs::withCount('circle')->where('online', '>', 0)->find($design_id);
        if (!$design) {
            $this->error('没有数据', [], 2);
        }
        $design['service'] = DesignServices::getServicesByDesign($design_id, $this->auth->getUserId());
        $this->success('请求成功', ['design' => $design]);
    }

    /**
     * 预约时间是否忙碌
     */
    public function busy($design_id)
    {
        $date = $this->request->get('date');
        if (empty($date)) {
            $this->error('日期不能为空');
        }
        $order = Orders::getOrderByTime($design_id, $date);
        $this->success('请求成功', compact('order'));
    }

    /**
     * 造型师注册
     */
    public function create()
    {
        $data = $this->request->post();
        $validate = new CreateDesignValidate();
        if (!$validate->batch()->check($data)) {
            $this->error($validate->getError());
        }
        $design = Designs::where('phone', $data['phone'])->find();
        if ($design) {
            $this->error('此手机号已注册，请重新输入');
        }
        $res = Designs::create([
            'nickname' => $data['nickname'] ?? '',
            'phone' => $data['phone'],
            'intro' => $data['intro'] ?? '',
            'avatar' => $data['avatar'] ?? '',
            'birthday' => strtotime($data['birthday'] ?? ''),
            'createtime' => time(),
            'updatetime' => time()
        ]);
        $res ? $this->success('请求成功') : $this->error('注册失败，请联系管理员');
    }

    /**
     * 造型师预约列表
     */
    public function orders($design_id)
    {
        $date = $this->request->get('date');
        $status = $this->request->get('status');
        $page = $this->request->get('page');
        $length = $this->request->get('length');
        $orders = Orders::getOrdersByDesign($design_id, $date, $status, $length);
        $orders->items() ? $this->success('请求成功', ['orders' => $orders->items()]) : $this->error('没有数据', [], 2);
    }

    /**
     * 造型师
     */
    public function read($design_id)
    {
        $design = Designs::getById($design_id);
        if (!$design) {
            $this->error("造型师不存在");
        }
        $design['design_count'] = Circles::getCircleCount($design_id);
        $this->success("请求成功", compact("design"));
    }
}
